<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Env;

// +----------------------------------------------------------------------
// | Workerman设置 仅对 php think worker:server 指令有效
// +----------------------------------------------------------------------
return [
//    'worker_class'	=>	['app\http\Worker1','app\http\Worker2'],
    // 扩展自身需要的配置
    'protocol'       => 'tcp', // 协议 支持 tcp udp unix http websocket text
    'host'           => '0.0.0.0', // 监听地址
    'port'           => 2345, // 监听端口
    'socket'         => '', // 完整监听地址
    'context'        => [], // socket 上下文选项
    'worker_class'   => '', // 自定义Workerman服务类名 支持数组定义多个服务

    // 支持workerman的所有配置参数
    'name'           => 'thinkphp',
    'count'          => 1,
    'daemonize'      => true,
    'pidFile'        => Env::get('runtime_path') . 'worker.pid',

    // 支持事件回调
    // onWorkerStart
    'onWorkerStart'  => function ($worker) {
        echo 'is start';
    },
    // onWorkerReload
    'onWorkerReload' => function ($worker) {

    },
    // onConnect
    'onConnect'      => function ($connection) {
    $connection->send('你好吗');
        echo 'is lianjie';
    },
    // onMessage
    'onMessage'      => function ($connection, $data) {
    $connection->send('你好');
//        global $worker;
//        $data = json_decode($data,true);
//        if(!isset($connection->uid)){
//            $connection->uid = $data['id'];
//            $worker->uidConnections[$connection->uid] = $connection;
//            $connection->send(json_encode(['messge'=>'链接成功']));
//        }
//        if($worker->uidConnections&&isset($data['to'])&&$data['to']=='all'){
//            foreach ($worker->uidConnections as $connectione){
//                if(isset($data['name'])&&isset($data['messge'])){
//                    $connectione->send(json_encode(['name'=>$data['name'],'messge'=>$data['messge']]));
//                }
//            }
//        }elseif ($worker->uidConnections&&isset($data['to'])&&$data['to']!=='all'){
//            if(isset($worker->uidConnections[$data['to']])){
//                $worker->uidConnections[$data['to']]->send(json_encode(['name'=>$data['name'],'messge'=>$data['messge']]));
//            }
//        }
//        print_r($data);
//        $connection->send(json_encode(['messge'=>'hha']));
//        $connection->send(json_encode(['messge'=>$data['messge']]));
//        foreach ($worker->uidConnections as $connectione){
//            $connectione->send(json_encode(['name'=>$data['name'],'messge'=>$data['messge']]));
//        }
//        $connection->send(json_encode(['name'=>$data['name'],'messge'=>$data['messge']]));
    },
    // onClose
    'onClose'        => function ($connection) {
//        echo 'guanbi';
    },
    // onError
    'onError'        => function ($connection, $code, $msg) {
//        echo "error [ $code ] $msg\n";
    },
];
